package org.xworker.meta_weave;

import org.eclipse.jetty.server.Server;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import xworker.lang.ai.utils.AIUtils;
import xworker.lang.executor.Executor;
import xworker.lang.util.XWorkerUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MetaWeaveMain {
    private static final String TAG = MetaWeaveMain.class.getName();

    public static void main(String[] args) {
        try {
            World world = World.getInstance();
            world.init("./xworker/");

            //添加模型管理器，即模型存放的目录，编辑模型需要。Test是模型管理器的名字，不能和其它模型管理重复。
            File dir = new File("./src/main/resources");
            if (dir.exists()) {
                //开发模式下
                world.addFileThingManager("MetaWeave", new File("./src/main/resources"), false, true);
            }

            //检查是否需要初始化，比如依赖模型库更新了重新建立索引
            XWorkerUtils.setup(false);

            /*
            File webXml = new File("./xworker/webroot/WEB-INF/web.xml");
            if (!webXml.exists()) {
                Map<String, String> resourceMap = new HashMap<>();
                //说明是通过代码启动的，因为发布后在XWorkerUtils.setup()就已经解压过了
                File zipResource = new File("./src/main/resources/xworker_resources.zip");
                try (FileInputStream fin = new FileInputStream(zipResource)) {
                    ZipInputStream zin = new ZipInputStream(fin);
                    unzipToXWorker(zin, resourceMap, false);
                    zin.close();
                }
            }*/

            //确保配置文件都在
            AIUtils.initServiceConfigThings();

            Thing jettyServer = world.getThing("org.xworker.meta_weave.WorkbenchSWT");
            Server server = jettyServer.doAction("run", new ActionContext());

            while (!server.isStarted()) {
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void unzipToXWorker(ZipInputStream zipInputStream, Map<String, String> resources, boolean verbose) throws IOException {
        ZipEntry entry;
        String webroot = World.getInstance().getPath() + "/";
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if(entry.isDirectory()){
                continue;
            }

            File outFile = new File(webroot + entry.getName());
            resources.put(entry.getName(), entry.getName());
            if (!outFile.getParentFile().exists()) {
                if(!outFile.getParentFile().mkdirs()){
                    throw new IOException("Can not create directory " + outFile.getParentFile());
                }
            }

            try (FileOutputStream fout = new FileOutputStream(outFile)) {
                byte[] bytes = new byte[4096];
                int length = -1;
                while ((length = zipInputStream.read(bytes)) != -1) {
                    fout.write(bytes, 0, length);
                }
            }
            if(verbose){
                Executor.info(TAG, "Unzip file to " + outFile.getPath());
            }

            zipInputStream.closeEntry();
        }
    }
}
